package bungaeting.net.client.android.activity;

import java.util.ArrayList;

import bungaeting.net.client.android.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class group extends Activity {	
	private static final String KEY_PREF = "group";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        onGroup();
    }
    
    public void onGroup() {
    	final Intent groupIndent = new Intent(this, groupManage.class);
		final ArrayList<String> group = new ArrayList<String>();
		group.add("��� ����ó");
		
		ClientDb mHelper = new ClientDb(this);
		SQLiteDatabase db = mHelper.getWritableDatabase();
      
		Cursor cursor = db.rawQuery("SELECT * FROM groupInfo", null);
		startManagingCursor(cursor);
      
		SimpleCursorAdapter simpleAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, new String[] {"groupName"} ,
				new int[] { R.id.label });
		
		final ListView adapter = (ListView)findViewById(R.id.left_list);
        adapter.setAdapter(simpleAdapter);
        adapter.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        adapter.setOnItemClickListener(new ListView.OnItemClickListener() {
	    	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	            startActivity(groupIndent);
	    	}
	    });
    }
    
    public void groupClick(View v) {
        Intent map = new Intent(this, group.class);
        startActivity(map);
    }
    
    public void mapClick(View v) {
        Intent map = new Intent(this, googleMap.class);
        startActivity(map);
    }
    
    public void groupAddClick(View v) {
    	AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
    	LayoutInflater inflater = getLayoutInflater();
    	final View view = inflater.inflate(R.layout.dialog, null);
    	final EditText editTextDialog = (EditText) view.findViewById(R.id.editTextDialog);
    	final ClientDb mHelper = new ClientDb(this);
    	final SQLiteDatabase db = mHelper.getWritableDatabase();
    	
    	alt_bld.setView(view);
    	
    	alt_bld.setCancelable(false).setPositiveButton("�߰�", 
    			 
    		new DialogInterface.OnClickListener() { 
	    		public void onClick(DialogInterface dialog, int id) {
	    			db.execSQL("INSERT INTO groupInfo VALUES (null, '"+ 
	    					editTextDialog.getText().toString() +"', null);");
	    			onGroup();
	    			dialog.cancel();
	    	}
	    }).setNegativeButton("�ƴϿ�", 
    			 
    		new DialogInterface.OnClickListener() {
	    		public void onClick(DialogInterface dialog, int id) {
	    		dialog.cancel();
	    	}
	    }); 
    	
    	AlertDialog dialog = alt_bld.create();  
    	
    	dialog.setTitle("�׷� �߰�");
    	dialog.show(); 
    }
}